<?php
/**
 * 《注塑安排》实现类
 *
 * @author 孙晓晔
 * @version $Id: Info.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/ZsUtil.class.php');

class Plan extends ZsUtil {
	public $tbl = Constant::tbl_zs_plan;

	public $status = array(
		'-' => '请选择',
		'1' => '等待注塑',
		'2' => '正在注塑',
		'3' => '完成注塑',

		'4' => '暂停注塑',
		'5' => '取消注塑',	

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',		
		'machine' => '机器',
		'worker' => '员工',
		'mold' => '产品',		
		'amount' => '数量',
		't1' => '开始时间',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'emp' => '员工',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('type_option', $this->type);
	}
	
	function mobile($controller, $request) {
		$this->pretreat($controller);
	
		global $CategoryTree;
	
		$data = $CategoryTree['注塑管理'];
	
		$ret = array();
		foreach ($data as $k => $v) {
			if ($this->tree_my($v)) {
				$ret[$k] = $v;
			}
		}
	
		$controller->assign('menu', $ret);
	
		$controller->display($request, 'mobile');
		return true;
	}
	
	function tab($controller, $request) {		
		$data = array(
			'注塑需求' => array('cc'=>'zs.php?do=Requirement.find', 'js'=>''),
			'注塑安排' => array('cc'=>'zs.php?do=Plan.find', 'js'=>''),
				
			'机台信息' => array('cc'=>'car.php?do=Injection.find', 'js'=>''),
		
			'统计分析' => array('cc'=>'zs.php?do=Plan.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'注塑需求' => array('cc'=>'zs.php?do=Requirement.group', 'js'=>''),
			'注塑安排' => array('cc'=>'zs.php?do=Plan.group', 'js'=>''),				
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));

		$controller->display($request, 'zs/plan/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 't1') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['t1'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$type_s = $request['type_s'];
		
		$name_s = trim(addslashes($request['name_s']));
		
		$machine_s = $this->_input($request['machine_s']);		
		$worker_s = $this->_input($request['worker_s']);

		$date1_s = $request['date1_s'];
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " where " . $my;
		}
		
		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content LIKE '%" . $name_s . "%')";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		if (!empty($machine_s)) {
			$where .= " AND machine = '" . $machine_s . "'";
		}
		
		if (!empty($worker_s)) {
			$where .= " AND worker = '" . $worker_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND DATE_FORMAT(t1, '%Y-%m-%d') <= '" . $date1_s . "' AND DATE_FORMAT(t2, '%Y-%m-%d') >= '" . $date1_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	function _my() {
		return '';
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['num_'] = $this->_num($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->assign('emp', $_SESSION['id']);
		$controller->assign('empname', $_SESSION['name']);

		$controller->display($request, 'zs/plan/list');
		return true;
	}
	
	function _num($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE plan = '%s'";
		$sql = sprintf($fmt, Constant::tbl_zs_plan_item, $id);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}
	
	function machine($id) {
		if (empty($id)) {
			return '';
		}

		$row = Common::id(Constant::tbl_machine, $id);

		return sprintf("%s/%s", $row['name'], $row['weight']);
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 1) {
			return false;
		}
		
		if ($this->_audit('40')) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function _num2($id) {
		$requirement = $this->_requirement($id);
		if (is_array($requirement)) {
			return count($requirement);
		}

		return 0;
	}

	function _requirement($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '3' AND plan = '%s' ORDER BY id";
		$sql = sprintf($fmt, Constant::tbl_zs_requirement, $id);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);

			$row['date'] = $this->_date($row['t1'], $row['t2']);

			$ret[] = $row;
		}

		return $ret;
	}

	

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'zs/plan/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$machine = $this->_input($request['machine']);
		$worker = $this->_input($request['worker']);

		$mold = $this->_input($request['mold']);
		$type = $request['type'];
						
		$cavity = $request['cavity'];
		$period = $request['period'];
		$weight1 = $request['weight1'];
		$weight2 = $request['weight2'];
		$material = $request['material'];
		$color = $request['color'];
		$amount = $request['amount'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(machine, worker, mold, type, cavity, period, weight1, weight2, material, color, amount, t1, t2, content, emp, ";
		$fmt .= "createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $machine, $worker, $mold, $type, $cavity, $period, $weight1, $weight2, $material, $color, $amount, $t1, $t2, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);

		$id = $db->insert_id();
		if (empty($code)) {
			$code = $this->_code();
		}

		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		$db->query($sql);

		$request['id_s'] = $id;
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);

		$controller->display($request, 'info');
		return true;
	}

	function _code() {
		$ret = '';
		$dd = sprintf("%s", date('yW00'));

		do {
			$dd++;
			$dd = sprintf("%06d", $dd);
			$ret = "Z" . $dd;
		} while ($this->_code_exist($ret));

		return $ret;
	}

	function _code_exist($code) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}

		return false;
	}

	function _machine($data, $machine, $worker, $content) {

		$cavity = $data['cavity'];
		$period = $data['period'];
		$weight1 = $data['weight1'];
		$weight2 = $data['weight2'];
		$material = $data['material'];
		$color = $data['color'];
		$amount = $data['amount'];
		
		$t1 = $data['t1'];
		$t2 = $data['t2'];
		$mold = $data['mold'];
		$type = $data['type'];
		
		$db = newdb($this->dsn_w);

		$fmt = "INSERT INTO %s(machine, worker, cavity, period, weight1, weight2, material, color, amount, t1, t2, content, mold, type, emp, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $machine, $worker, $cavity, $period, $weight1, $weight2, $material, $color, $amount, $t1, $t2, $content, $mold, $type, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		$plan = $db->insert_id();

		$code = $this->_code();

		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $plan);
		$db->query($sql);		

		return $plan;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['item'] = json_encode($this->_plan_item($row['id']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/plan/edit');
		return true;
	}
	
	function _plan_item($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/PlanItem.class.php');
		$aa = new PlanItem();
		
		return $aa->_list($id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];

		$machine = $this->_input($request['machine']);
		$worker = $this->_input($request['worker']);

		$mold = $this->_input($request['mold']);
		$type = $request['type'];
						
		$cavity = $request['cavity'];
		$period = $request['period'];
		$weight1 = $request['weight1'];
		$weight2 = $request['weight2'];
		$material = $request['material'];
		$color = $request['color'];
		$amount = $request['amount'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET machine = '%s', worker = '%s', mold = '%s', type = '%s', cavity = '%s', period = '%s', weight1 = '%s', weight2 = '%s', ";
		$fmt .= "material = '%s', color = '%s', amount = '%s', t1 = '%s', t2 = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $machine, $worker, $mold, $type, $cavity, $period, $weight1, $weight2, $material, $color, $amount, $t1, $t2, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$this->_update_plan_item($request);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_plan_item($request) {
		
		require_once(dirname(__FILE__) . '/PlanItem.class.php');
		$aa = new PlanItem();
		
		return $aa->_update($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['log'] = $this->_plan_item($row['id']);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/plan/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['machine_t'] = Name::machine($row['machine']);
		$row['worker_t'] = Name::emp($row['worker']);

		// $row['date'] = $this->_date($row['t1'], $row['t2']);

		$row['mold_t'] = Name::mold($row['mold']);
		$row['type_t'] = $this->type[$row['type']];
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->id($this->tbl, $id);
			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function finish($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/plan/finish');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function finishPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$tt = $request['tt'];
		$cost = $request['cost'];

		// $content = sprintf("%s\n%s 操作", addslashes($request['content']), Name::emp($_SESSION['id']));
		$content = addslashes($request['content']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET tt = '%s', cost = '%s', content = concat(content, '%s'), status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $tt, $cost, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _name($id) {
		$row = $this->id(Constant::tbl_quote, $id);

		$status = $row['status'];
		if ($status == '3') {
			return true;
		}

		if ($status == '5') {
			return true;
		}

		if ($status == '8') {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_code($controller, $request) {
		
		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND code LIKE '%s'";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['machine_t'] = Name::machine($row['machine']);
			$row['worker_t'] = Name::emp($row['worker']);

			$row['date'] = $this->_date($row['t1'], $row['t2']);

			$ret[] = $row;
		}

		return $ret;
	}
	
	function _list() {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' ORDER BY id DESC LIMIT 50";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['machine_t'] = Name::machine($row['machine']);
			$row['worker_t'] = Name::emp($row['worker']);

			$row['date'] = $this->_date($row['t1'], $row['t2']);

			$ret[] = $row;
		}

		return $ret;
	}	

	function ajax_valid($controller, $request) {
		$machine = $this->_input($request['machine']);
		$worker = $this->_input($request['worker']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$ret = array();
		$a = $this->_valid_machine($t1, $t2, $machine);
		if (!$a) {
			$ret['status'] = 1;
			echo json_encode($ret);
			return false;
		}

		$b = $this->_valid_worker($t1, $t2, $worker);
		if (!$b) {
			$ret['status'] = 2;
			echo json_encode($ret);
			return false;
		}

		$ret['status'] = 3;

		echo json_encode($ret);
	}

	function _valid_time($row, $t1, $t2) {
		if (strtotime($t1) > strtotime($row['t1']) && strtotime($t1) < strtotime($row['t2'])) {
			return false;
		}

		if (strtotime($t2) > strtotime($row['t1']) && strtotime($t2) < strtotime($row['t2'])) {
			return false;
		}

		if (strtotime($t1) <= strtotime($row['t1']) && strtotime($t2) >= strtotime($row['t2'])) {
			return false;
		}

		return true;
	}

	function _valid_machine($t1, $t2, $machine) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE machine = '%s' AND status = 1 AND createtime > DATE_ADD(now(), INTERVAL -20 DAY)";
		$sql = sprintf($fmt, $this->tbl, $machine);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			if (!$this->_valid_time($row, $t1, $t2)) {
				return false;
			}
		}

		return true;
	}

	function _valid_worker($t1, $t2, $worker) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE worker = '%s' AND status = 1 AND createtime > DATE_ADD(now(), INTERVAL -20 DAY)";
		$sql = sprintf($fmt, $this->tbl, $worker, $t1, $t2);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			if (!$this->_valid_time($row, $t1, $t2)) {
				return false;
			}
		}

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reason($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['machine_t'] = Name::machine($row['machine']);
		$row['worker_t'] = Name::emp($row['worker']);

		$row['date'] = $this->_date($row['t1'], $row['t2']);

		$row['mold_t'] = Name::mold($row['mold']);
		$row['type_t'] = $this->type[$row['type']];

		$row['emp_t'] = Name::emp($row['emp']);
		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/plan/reason');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reasonPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$reason = addslashes($request['reason']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET reason = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$fmt = "UPDATE %s SET reason = '%s', status = '%s', modifytime = now() WHERE status = '3' AND plan = '%s'";
		$sql = sprintf($fmt, Constant::tbl_zs_requirement, $reason, $status, $id);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status_continue($controller, $request) {

		return $this->status_restart($controller, $request);
	}

	function status_restart($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, '1', $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
}
?>